package com.demo;

import com.demo.domain.Author;

import java.util.Optional;

/**
 * @author gxd
 * @since 2025/6/2
 */
public class OptionalDemo {
    public static void main(String[] args) {
        // optional 避免空指针异常
        // 创建对象 一般使用optional的静态方法ofNullable(推荐使用)
        Author author = getAuthor();
        Optional<Author> authorOptional = Optional.ofNullable(author);
        authorOptional.ifPresent(author1 -> System.out.println(author1.getName()));

        Optional<Author> authorOptional1 = getAuthorOptional();
        authorOptional1.ifPresent(author1 -> System.out.println(author1.getName()));

        // 如果确定一个对象不是空，则可以使用Optional的静态方法of
        Optional<Author> authorOptional2 = Optional.of(author);
        authorOptional2.ifPresent(author1 -> System.out.println(author1.getName()));
        // 如果结果是null会报错
        // Optional<Author> authorOptional2 = Optional.of(null);

        // Optional的静态方法empty, 参考 getAuthorOptional2

        // 安全消费值 ifPresent方法
        // 这个方法会判断其内的数据是否为空，不为空才会执行具体的消费代码。这样使用起来更加安全
        Optional<Author> authorOptional3 = Optional.ofNullable(author);
        authorOptional3.ifPresent(author1 -> System.out.println(author1.getName()));

        // 获取值 get方法 （不推荐） 数据为空时报错异常
        Optional<Author> authorOptional4 = Optional.ofNullable(author);
        Author author4 = authorOptional4.get();

        // 安全获取值
        // orElseGet：获取数据并且设置数据为空的默认值。如果数据不为空就获取到该数据，如果为空则根据你传入的参数来创建对象作为默认返回值
        Optional<Author> authorOptional5 = getAuthorOptional();
        Author author2 = authorOptional5.orElseGet(() -> new Author(2L, "李四", 18, "蠢萌大学生", null));
        System.out.println(author2.getName());
        // orElseThrow：获取数据，如果数据不为空就能获取到该数据。如果为空则根据你传入的参数来创建异常抛出
        Optional<Author> authorOptional6 = getAuthorOptional();
        try {
            Author author3 = authorOptional6.orElseThrow(() -> new RuntimeException("空指针"));
            System.out.println(author3);
        } catch (Throwable e) {
            e.printStackTrace();
        }

        // 过滤 filter方法
        Optional<Author> authorOptional7 = getAuthorOptional();
        authorOptional7.filter(author1 -> author1.getAge() > 18).ifPresent(author1 -> System.out.println(author1.getName()));

        // 判断 isPresent方法
        Optional<Author> authorOptional8 = getAuthorOptional();
        if (authorOptional8.isPresent()) {
            System.out.println(authorOptional8.get().getName());
            System.out.println(authorOptional8.get().getAge());
        }

        // 数据转换 map方法
        Optional<Author> authorOptional9 = getAuthorOptional();
        authorOptional9.map(author1 -> author1.getName()).ifPresent(System.out::println);
    }

    public static Optional<Author> getAuthorOptional2() {
        Author author = new Author(1L, "张三", 26, "一个法外狂徒", null);
        return author == null ? Optional.empty() : Optional.of(author);
    }

    public static Optional<Author> getAuthorOptional() {
        Author author = new Author(1L, "张三", 26, "一个法外狂徒", null);
        return Optional.ofNullable(author);
    }

    public static Author getAuthor() {
        Author author = new Author(1L, "张三", 26, "一个法外狂徒", null);
        return author;
        // return null;
    }
}
